package com.rh.bmw4s.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rh.bmw4s.R;
import com.rh.bmw4s.bean.BaseBean;
import com.rh.bmw4s.bean.RegisterBean;
import com.rh.bmw4s.comm.Constant;
import com.rh.bmw4s.utills.CheckUtil;
import com.rh.bmw4s.utills.DealResult;
import com.rh.bmw4s.utills.DialogUtils;
import com.rh.bmw4s.utills.LogUtils;
import com.rh.bmw4s.utills.TimeCount;
import com.rh.bmw4s.utills.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PswFindActivity extends BaseActivity {


    @BindView(R.id.img_head)
    ImageView imgHead;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.tv_get_yzm)
    TextView tvGetYzm;
    @BindView(R.id.et_psw_confirm)
    EditText etPswConfirm;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.view_1)
    View view1;
    @BindView(R.id.et_reg_name)
    EditText etRegName;
    @BindView(R.id.et_reg_phone)
    EditText etRegPhone;
    @BindView(R.id.et_reg_psw)
    EditText etRegPsw;
    private TimeCount timeCount;

    private Gson gson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_psw_find);
        ButterKnife.bind(this);
        initView();
        gson = new Gson();
    }

    private void initView() {
        timeCount = new TimeCount(tvGetYzm, 2 * 60 * 1000, 1000);
        etRegName.setVisibility(View.GONE);
        view1.setVisibility(View.GONE);
        tvRegister.setText("修改密码");

        //etRegPsw.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //明文
        etRegPsw.setTransformationMethod(PasswordTransformationMethod.getInstance());     //密文
        etPswConfirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }

    @OnClick({R.id.tv_get_yzm, R.id.tv_register, R.id.tv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_yzm:
                String phone = etRegPhone.getText().toString();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtils.toastForShort(PswFindActivity.this, "请先输入手机号码");
                    return;
                }
                getYzm(phone);
                break;
            case R.id.tv_register:
                getInputDatas();
                break;
            case R.id.tv_back:
                this.finish();
                break;
        }
    }

    private void getYzm(String phone) {
        OkGo.<String>post(Constant.YZM)
                .tag(this)
                .params("mobile", phone)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        BaseBean<String> bean = DealResult.getInstace().dealData(PswFindActivity.this, response, new TypeToken<BaseBean<String>>() {
                        }.getType());
                        timeCount.start();
                        ToastUtils.toastForShort(PswFindActivity.this, bean.getMsg());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(PswFindActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }

    private void getInputDatas() {
//        String name= etName.getText().toString();
        String phone = etRegPhone.getText().toString();
        String yzm = etYzm.getText().toString();
        String psw = etRegPsw.getText().toString();
        String pswConfirm = etPswConfirm.getText().toString();
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(yzm) || TextUtils.isEmpty(psw) || TextUtils.isEmpty(pswConfirm)) {
            ToastUtils.toastForShort(PswFindActivity.this, "请完善信息");
            return;
        }
        if (!CheckUtil.checkPassword(psw) || psw.length() < 6) {
            ToastUtils.toastForShort(PswFindActivity.this, "密码格式不正确");
            return;
        }
        if (!TextUtils.equals(psw, pswConfirm)) {
            ToastUtils.toastForShort(PswFindActivity.this, "密码不一致");
            return;
        }
        register(phone, yzm, psw);
    }

    private void register(String phone, String yzm, String psw) {
        OkGo.<String>post(Constant.FIND_PSW)
                .tag(this)
                .params("phone", phone)
                .params("password", psw)
                .params("yanpass", yzm)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        LogUtils.d("eee", response.body().toString());

                        RegisterBean registerBean = gson.fromJson(response.body().toString(), RegisterBean.class);
                        ToastUtils.toastForShort(PswFindActivity.this, registerBean.getMsg());
                        if (registerBean.getStatus() == Constant.SUCESS) {
                            //找回密码成功，返回登录界面
                            startActivity(new Intent(PswFindActivity.this, LoginRegActivity.class));
                            PswFindActivity.this.finish();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(PswFindActivity.this, "找回密码失败");
                        super.onError(response);
                    }
                });
    }

}
